package me.zhengjie.modules.system.service.impl;

import cn.hutool.json.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.domain.vo.DeptQueryCriteria;
import me.zhengjie.modules.system.domain.vo.FlowConfigQueryCriteria;
import me.zhengjie.modules.system.domain.vo.FlowTypeQueryCriteria;
import me.zhengjie.modules.system.mapper.DeptMapper;
import me.zhengjie.modules.system.mapper.FlowConfigMapper;
import me.zhengjie.modules.system.mapper.FlowTypeMapper;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.modules.system.service.ApplyListService;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RuntimeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ApplyListServiceImpl implements ApplyListService{

    private final FlowTypeMapper flowTypeMapper;
    private final FlowConfigMapper flowConfigMapper;
    private final IdentityService identityService;
    private final UserMapper userMapper;
    private final DeptMapper deptMapper;
    private  final ProcessEngine processEngine;

    @Override
    public List<FlowType> getProcessDefine() {
        List<FlowType> flowTypes = flowTypeMapper.findAll(new FlowTypeQueryCriteria());
        List<FlowConfig> flowConfigs = flowConfigMapper.findAll(new FlowConfigQueryCriteria());
        List<FlowType> flows = new ArrayList<>();
        flowTypes.forEach( item->{
            List<FlowConfig> flowConfigs1 = new ArrayList<>();
            FlowType flowType = new FlowType();
            flowType.setFlowType(item.getFlowType());
            flowType.setId(item.getId());
            flowConfigs.forEach( item1->{
                FlowConfig flowConfig = new FlowConfig();
                if(item.getId().equals(item1.getFlowTypeId()) && item1.getIsDeployed().equals("1")){
                    flowConfig.setId(item1.getId());
                    flowConfig.setFlowId(item1.getFlowId());
                    flowConfig.setFlowName(item1.getFlowName());
                    flowConfig.setFlowKey(item1.getFlowKey());
                    flowConfig.setIcon(item1.getIcon());
                    flowConfigs1.add(flowConfig);
                }
            });
            flowType.setFlowConfig(flowConfigs1);
            flows.add(flowType);
        });
        return flows;
    }

    @Override
    public List<User> getApproveUsers(Long id) {
        FlowConfig flowConfig = flowConfigMapper.findById(Math.toIntExact(id));
        String orginalData = flowConfig.getOrginalData();
        JSONObject jsonObject = JSONObject.parseObject(orginalData);

        DeptQueryCriteria deptQueryCriteria = new DeptQueryCriteria();
        Dept dept = deptMapper.findAll(deptQueryCriteria).get(0);
        return userMapper.findByRoleDeptId(dept.getId());
    }

    @Override
    public void initiateProcess(Map<String,Object> map) {
        try {
            String flowId = map.get("flowId").toString();
            Map<String,Object> variables = (Map<String, Object>) map.get("variables");
            identityService.setAuthenticatedUserId(map.get("userId").toString());
            RuntimeService runtimeService = processEngine.getRuntimeService();
            runtimeService.startProcessInstanceById(flowId, variables);
        }catch (Exception e){
            throw new RuntimeException("启动流程失败");
        }
    }
}
